﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STF.OrangeDog.IService;
using STF.OrangeDog.IRepository;
using STF.OrangeDog.Dtos;
using STF.OrangeDog.Model;
using System.Threading.Tasks;

namespace STF.OrangeDog.Service
{
    public class UsersBll
    {
        private IUsersDal _db;

        public UsersBll(IUsersDal db)
        {
            _db = db;
        }
        public async Task<int> AddUsersAsync(string Email, string Pwd, string NickName, int StateCode)
        {
            return await _db.AddModelAsync(new Users
            {
                Email = Email,
                Pwd = Pwd,
                NickName = NickName,
                StateCode = StateCode
            });
        }

        public async Task<int> DeleteUsersAsync(int Id)
        {
            var data = await _db.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            if (data.DeleteId == 1)
                return 0;

            return await _db.RemoveModelAsync(data);
        }

        public async Task<int> EditUsersAsync(int Id, string Email, string Pwd, string NickName, int StateCode)
        {
            var data = await _db.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            data.Email = Email;
            data.Pwd = Pwd;
            data.NickName = NickName;
            data.StateCode = StateCode;
            data.UpdateTime = DateTime.Now;
            return await _db.EditModelAsync(data);
        }

        public async Task<IEnumerable<UsersDtos>> GetAll(int DeleteId = 1)
        {
            var data = await _db.GetDataByChooseAsync(r => r.DeleteId == DeleteId);
            return data != null ? data.Select(r => new UsersDtos
            {
                id = r.Id,
                Email = r.Email,
                Pwd = r.Pwd,
                NickName = r.NickName,
                StateCode = r.StateCode,
               
            }) : new List<UsersDtos>();
        }

        public async Task<UsersDtos> GetDataById(int Id)
        {
            var data = await _db.GetDataByIdAsync(Id);
            return data == null ? new UsersDtos() : new UsersDtos
            {
                id = data.Id,
                Email = data.Email,
                Pwd = data.Pwd,
                NickName = data.NickName,
                StateCode = data.StateCode,
                
            };
        }
    }
}
